//
//  CityListViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/1.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "CityListViewController.h"
#import "HeaderFileForEasyToTesco.h"
<<<<<<< HEAD
//数据解析的头文件
#import "AFNetworking.h"



=======

//数据解析的头文件
#import "AFNetworking.h"

>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
@interface CityListViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger headertemp;
    BOOL headerbool;
}

@property (nonatomic, strong)UITableView *citylistTableview;

//存放省份的数组列表
@property (nonatomic, strong)NSMutableArray *ProvincesArray;

//存放的是城市的数组
@property (nonatomic, strong)NSMutableArray *citysArray;
<<<<<<< HEAD
@property (weak,nonatomic) UILabel *province;
@property (weak,nonatomic) UILabel *city;
@property (weak,nonatomic) UILabel *county;
=======

>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
@end

@implementation CityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
<<<<<<< HEAD
 
 //   [self addinitSomething];
    
    [self addcityListtopview];
    //  [self addcitytableview];
    

    
    
=======
    
    [self addinitSomething];
    
    [self addcityListtopview];
    
    [self addcitytableview];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
}

- (void)addcityListtopview {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"城市列表";

}
- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addcitytableview {
    self.citylistTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.9) style:UITableViewStyleGrouped];
    self.citylistTableview.delegate = self;
    self.citylistTableview.dataSource = self;
    self.citylistTableview.rowHeight = 30;
    self.citylistTableview.tableFooterView =[[UIView alloc] init];
    [self.view addSubview:self.citylistTableview];
}

- (void)addinitSomething {
    headertemp = -1;
    headerbool = YES;
<<<<<<< HEAD
=======
    
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    self.ProvincesArray = [@[]mutableCopy];
    self.citysArray = [@[]mutableCopy];
    
    
    [self addcitylistDatasources];
}


- (void)addcitylistDatasources{
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    __weak CityListViewController *citySelf = self;
    
[mgr GET:@"http://www.ylgw8.com/AppCity/Getpcc" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    
    NSMutableDictionary *dic = [citySelf toArrayOrNSDictionary:responseObject];
    
    self.ProvincesArray = [@[]mutableCopy];
    self.citysArray = [@[]mutableCopy];
    
    self.ProvincesArray = dic[@"province"];
    
    citySelf.citysArray = [NSMutableArray arrayWithArray:dic[@"city"]];

    NSMutableArray *tempaArray = [@[]mutableCopy];
    
    for (NSInteger i = 0; i<self.ProvincesArray.count; i++) {
        NSString *tempstr = [NSString stringWithFormat:@"%@", [self.ProvincesArray[i] objectForKey:@"ProID"]];
        
        NSLog(@"-=-=-%@", tempstr);
        
        NSMutableArray *hhArray =[@[]mutableCopy];
        NSMutableDictionary *dicts = [[NSMutableDictionary alloc] initWithCapacity:0];
        
        [dicts setValue:[NSString stringWithFormat:@"%@", [self.ProvincesArray[i] objectForKey:@"ProID"]] forKey:@"ProID"];//
        
        [dicts setValue:[NSString stringWithFormat:@"%@", [self.ProvincesArray[i] objectForKey:@"name"]] forKey:@"name"];
        
        [dicts setValue:[NSString stringWithFormat:@"%@", [self.ProvincesArray[i] objectForKey:@"ProSort"]] forKey:@"ProSort"];
        
        
        for (NSInteger j = 0; j < self.citysArray.count; j++) {
            NSString *tempStr2 = [NSString stringWithFormat:@"%@", [self.citysArray[j] objectForKey:@"ProID"]];
            if ([tempStr2 isEqualToString:tempstr]) {
                [hhArray addObject:self.citysArray[j]];
                [dicts setValue:hhArray forKey:@"city"];//
            }
        }
        [tempaArray addObject:dicts];
    }
    
    self.ProvincesArray = [[NSMutableArray alloc] initWithArray:tempaArray];
    
    [self.citylistTableview reloadData];
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (headerbool == YES) {
        return 0;
    } else {
    if (headertemp == section) {
        return [[self.ProvincesArray[section] objectForKey:@"city"] count];
    } else {
        return 0;
    }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cityListCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [[[self.ProvincesArray[indexPath.section] objectForKey:@"city"] objectAtIndex:indexPath.row] objectForKey:@"name"]];
        return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.ProvincesArray.count;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerview = [[UIView alloc] init];
    
    UILabel *provincelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, WEITH-20, 30)];
    provincelabel.text = [NSString stringWithFormat:@"%@", [self.ProvincesArray[section] objectForKey:@"name"]];
    provincelabel.font = [UIFont systemFontOfSize:20];
    [headerview addSubview:provincelabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, WEITH, 40);
    [button addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 340+section;
    [headerview addSubview:button];
    return headerview;
}

- (void)headerBtnClick:(UIButton *)button{
    if (headerbool == YES) {
        headertemp = button.tag - 340;
       
        headerbool = NO;
    } else {
        headerbool = YES;
        headertemp = -1;
    }
     [self.citylistTableview reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    return footview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"=======");
    
    NSString *city = [self.ProvincesArray[indexPath.section] objectForKey:@"name"];
    NSString *county = [[[self.ProvincesArray[indexPath.section ] objectForKey:@"city"] objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"省份%@,县市%@", city, county] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alview show];
    
}






// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
